<!DOCTYPE html>
<html>

	<head>
	  <meta charset="utf-8">

	  <title>AIL - framework</title>
		<link rel="icon" href="{{ url_for('static', filename='image/ail-icon.png') }}">

	  <!-- Core CSS -->
		<link href="{{ url_for('static', filename='css/bootstrap4.min.css') }}" rel="stylesheet">
		<link href="{{ url_for('static', filename='css/font-awesome.min.css') }}" rel="stylesheet">
        <link href="{{ url_for('static', filename='css/tags.css') }}" rel="stylesheet" type="text/css"/>

	  <!-- JS -->
		<script src="{{ url_for('static', filename='js/jquery.js')}}"></script>
		<script src="{{ url_for('static', filename='js/helper.js')}}"></script>
		<script src="{{ url_for('static', filename='js/bootstrap4.min.js')}}"></script>
        <script src="{{ url_for('static', filename='js/tags.js') }}"></script>

	</head>
	<body>

		{% include 'nav_bar.html' %}

		<div class="container-fluid">
			<div class="row">

				{% include 'sidebars/sidebar_objects.html' %}

				<div class="col-12 col-lg-10" id="core_content">

					<div class="card mb-3 mt-1">
						<div class="card-header text-white bg-dark">
							<h5 class="card-title"><img src="{{ url_for('static', filename='image/misp-logo.png')}}" alt="MISP" style="width:100px;"> MISP Exporter</h5>
						</div>
						<div class="card-body">

							<form action="{{ url_for('import_export.objects_misp_export_post') }}" method="post" enctype=multipart/form-data onsubmit="submitPaste()">

								<div id="container-id-to-import">

									<p>Select a list of objects to export</p>

									<div class="row">
										<div class="col-2" for="obj_input_type"><b>Object Type</b></div>
										<div class="col-8" for="obj_input_id"><b>Object ID</b></div>
										<div class="col-1" for="obj_input_lvl"><b>Lvl</b></div>
									</div>

									<div class="form-horizontal">
                                        <div class="form-body">
                                            <div class="form-group">
                                                <div class="fields">
                                                    <div class="input-group mb-1">
                                                        <select class="custom-select col-2" name="first_obj_to_export" id="obj_input_type">
                                                            <option selected>Object type...</option>
                                                            {% for object_type in object_types %}
                                                                <option value="{{ object_type[0] }}:{{ object_type[1] }}">
                                                                    {% if object_type[1] %}
                                                                        {{ object_type[0] }} - {{ object_type[1] }}
                                                                    {%  else %}
                                                                        {{ object_type[0] }}
                                                                    {% endif %}
                                                                </option>
                                                            {% endfor %}
                                                        </select>

                                                        <input type="text" class="form-control col-8" name="first_obj_to_export" id="obj_input_id">
														<input class="form-control col-1" type="number" min="0" value="0" name="first_obj_to_export" id="obj_input_lvl">
									                    <span class="btn btn-info input-group-addon add-field col-1"><i class="fas fa-plus"></i></span>
									                </div>

													{% for obj_dict in to_export %}
														{% with obj_type=obj_dict['type'], obj_subtype=obj_dict['subtype'], obj_id=obj_dict['id'], obj_lvl=obj_dict['lvl'], input_uuid=obj_dict, obj_error=False%}
															{% include 'import_export/block_to_export_input.html' %}
														{% endwith %}
													{% endfor %}

													<br>

													{% for obj_dict in l_obj_invalid %}
														{% with obj_type=obj_dict['type'], obj_subtype=obj_dict.get('subtype', None), obj_id=obj_dict['id'], obj_lvl=obj_dict['lvl'], input_uuid=obj_dict, obj_error=True%}
															{% include 'import_export/block_to_export_input.html' %}
														{% endwith %}
													{% endfor %}
									        <span class="help-block" hidden>Export Objects</span>
									      </div>
									    </div>
									  </div>
								</div>

							</div>

							<div class="d-flex mb-3">
								JSON Export&nbsp;
								<div class="custom-control custom-switch">
									<input class="custom-control-input" type="checkbox" name="export_to_misp" value="True" id="export_to_misp">
									<label class="custom-control-label" for="export_to_misp">
										Export to MISP Instance
									</label>
								</div>
							</div>

							<div class="card border-dark my-3 px-3 py-3" id="export_to_misp_div">
								{% include 'import_export/block_create_misp_event.html' %}
							</div>

							<div class="form-group">
								<button class="btn btn-info" type="submit">Export Objects</button>
							</div>

							</form>

						</div>
					</div>

					{% if misp_url %}
						MISP Event Created:
						<a target="_blank" href="{{  misp_url }}">
							{{  misp_url }}
						</a>
					{% endif %}

			</div>
		</div>

<script>
$(document).ready(function(){
		$("#page-Decoded").addClass("active");
		$("#nav_misp_export").addClass("active");
		export_to_misp_input_controler();

		$('#export_to_misp').change(function () {
			export_to_misp_input_controler();
		});
});

function toggle_sidebar(){
	if($('#nav_menu').is(':visible')){
		$('#nav_menu').hide();
		$('#side_menu').removeClass('border-right')
		$('#side_menu').removeClass('col-lg-2')
		$('#core_content').removeClass('col-lg-10')
	}else{
		$('#nav_menu').show();
		$('#side_menu').addClass('border-right')
		$('#side_menu').addClass('col-lg-2')
		$('#core_content').addClass('col-lg-10')
	}
}

var input_1 = '<div class="input-group mb-1"><select class="custom-select col-2" name="'
var input_2 = '"><option selected>Object type...</option>' +
    {% for object_type in object_types %}
        '<option value="{{ object_type[0] }}:{{ object_type[1] }}">' +
        {% if object_type[1] %}
            '{{ object_type[0] }} - {{ object_type[1] }}' +
        {%  else %}
            '{{ object_type[0] }}' +
        {% endif %}
        '</option>' +
    {% endfor %}
    '</select><input type="text" class="form-control col-8" name="'
var input_3 = '"><input class="form-control col-1" type="number" min="0" value="0" name="'
var input_4 = '"></div>';
var minusButton = '<span class="btn btn-danger input-group-addon delete-field col-1"><i class="fas fa-trash-alt"></i></span>';

$('.add-field').click(function() {
	var new_uuid = uuidv4();
	var template = input_1 + new_uuid + input_2 + new_uuid + input_3+ new_uuid + input_4;
    var temp = $(template).insertBefore('.help-block');
    temp.append(minusButton);
});

$('.fields').on('click', '.delete-field', function(){
    var obj = $(this).attr('id')
    if (obj) {
        obj = obj.split(":", 3);
        var params = "?type=" + obj[0] + "&subtype=" + obj[1] +"&id=" + obj[2];
        $.ajax({url: "{{ url_for('import_export.delete_object_id_to_export') }}" + params })
    }
    $(this).parent().remove();
});


function uuidv4() {
  return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
    (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  );
}

function export_to_misp_input_controler() {
	if($('#export_to_misp').is(':checked')){
			$("#export_to_misp_div").show();
		}else{
			$("#export_to_misp_div").hide();
		}
}

</script>


</body>

</html>
